#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int, int> dp;
        dp[0] = 1;
        int ans = 0, temp = 0;
        for (auto& it : nums) {
            temp += it;
            if (dp.find(temp - k) != dp.end()) {
                ans += dp[temp - k];
            }
            ++dp[temp];
        }
        return ans;
    }
};